package com.hk.yajin.function.members.activity.setting;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hk.developmentlib.baseben.NewWorkBean;
import com.hk.developmentlib.callback.HttpCallBack;
import com.hk.yajin.R;
import com.hk.yajin.base.BaseActivity;
import com.hk.yajin.manager.UserInfoManager;
import com.hk.yajin.utills.HttpRequestUtils;
import com.hk.yajin.utills.url.UrlConstants;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AcountSettingActivity extends BaseActivity {


    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_change_phone)
    TextView tvChangePhone;
    @BindView(R.id.tv_change_nickname)
    TextView tvChangeNickname;
    @BindView(R.id.tv_change_pwd)
    TextView tvChangePwd;
    /*邀请码*/
    @BindView(R.id.et_invite)
    TextView etInvite;
    @BindView(R.id.tv_submit_code)
    TextView tvSubmitCode;
    @BindView(R.id.lly_code)
    LinearLayout llyCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_acount_setting);
        ButterKnife.bind(this);
        intiView();
    }

    private void intiView() {
        initToolBar(R.string.myacount);
        if (UserInfoManager.getInstance().getUserInfo().is_bind_inviter()) {
            llyCode.setVisibility(View.GONE);
        } else {
            llyCode.setVisibility(View.VISIBLE);
        }
    }


    @OnClick({R.id.tv_change_phone, R.id.tv_change_nickname, R.id.tv_change_pwd, R.id.tv_submit_code})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_change_phone:
                startActivity(ChangePhoneActivity.class);
                break;
            case R.id.tv_change_nickname:
                startActivity(ChangeNickNameActivity.class);
                break;
            case R.id.tv_change_pwd:
                startActivity(ChangePasswordActivity.class);
                break;
            case R.id.tv_submit_code:
                String text = etInvite.getText().toString();
                if (!TextUtils.isEmpty(text)) {
                    httpSubmitInviteCode();
                }
                break;
        }
    }

    private void httpSubmitInviteCode() {
        if (isNetWorkConnect()) {
            showLoadingView();
            Map<String, Object> reqParam = UserInfoManager.getUserKeyMapIfLogin();
            reqParam.put("inviter", etInvite.getText().toString());
            HttpRequestUtils.post(false, UrlConstants.FUNCTION_GET_INVATE_CODE_SUBMIT, reqParam, false, new HttpCallBack() {

                @Override
                public void onSucced(String data) {
                    hideLoadingView();
                    showShortToast("提交成功");
                    UserInfoManager.getInstance().getUserInfo().setIs_bind_inviter(true);
                    llyCode.setVisibility(View.GONE);
                }

                @Override
                public void onErrorCode(NewWorkBean bean) {
                    hideLoadingView();
                    showShortToast(bean.getMsg());
                }

                @Override
                public void onBadHttp(String tip) {
                    hideLoadingView();
                    showShortToast(tip);
                }

            });
        }
    }
}
